<?php

class CommentsController extends Controller {
	var $name = 'Comments';
	var $uses = array('Comments');
	var $components = array('page', 'comments', 'posts');
    var $no_session = array('post');

	function index() {
		$this->action = 'comments';
		$this->comments();
	}

    function comments($page = 1, $status = '') {
        $results = get_comments($page);
        $this->set('results', $results);
        $this->set('url', $this->this_url());
        if ($status == 'deleted_done') {
            $this->header_notice = __('Your comments has been successfully deleted.'); 
        } else if ($status == 'deleted_failed') {
            $this->header_notice = __('Your comments deleted failed.'); 
        }
        $this->set('page', $page);
    }

# add new comment
	function post($id) {
        $this->layout = null;
        if (!empty($this->data)) {
            $this->data['Comments']['author'] = sanitize_title($this->data['Comments']['author']);
            $this->data['Comments']['author_email'] = sanitize_title($this->data['Comments']['author_email']);
            $this->data['Comments']['author_url'] = sanitize_title($this->data['Comments']['author_url']);
            $this->data['Comments']['author_ip'] = preg_replace('/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR']);
            $this->data['Comments']['author_agent'] = $_SERVER['HTTP_USER_AGENT'];
            #$this->data['Comments']['content'] = nl2br($this->data['Comments']['content']);
            $this->data['Comments']['post_id'] = (int)$this->data['Comments']['post_id'];
            $this->data['Comments']['post_time'] = time();
            $this->data['Comments']['type'] = 'post';
            $this->data['Comments']['status'] = 1;

            if (empty($this->data['Comments']['author'])) {
                die('用户名不能为空.');
            }
            if (empty($this->data['Comments']['author_email'])) {
                die('邮箱不能为空.');
            }
            if (!is_email($this->data['Comments']['author_email'])) {
                die('邮箱不能为空.');
            }
            if (empty($this->data['Comments']['content'])) {
                die('评论内容不能为空.');
            }
            $this->Comments->save($this->data);
            $comment_id = $this->Comments->get_last_insert_id();
            al_redirect($this->referer . '#comment-' . $comment_id);
        }
	}

    function ajax_delete_comments($ids = '') {
        $this->layout = null;
        $i = 0;
        if (!empty($ids)) {
            $ids = trim($ids, '|');
            $ids = explode('|', $ids);
            if (!empty($ids)) {
                foreach($ids as $id) {
                    $this->Comments->del($id);
                    $i++;
                }
            }
        }
        if (!empty($i)) {
            echo 'deleted_done';
        } else {
            echo 'deleted_failed';
        }
    }

    function delete($id = null) {
        if (empty($id)) {
            $this->error_404();
        }
        $this->Comments->del($id);
        $redirect = rtrim($this->referer, '/') . '/deleted_done';
        $this->redirect($redirect);
    }
    
    function this_url() {
        $url = str_replace('done', '', $this->url());
        $url = rtrim($url, '/');
        if ($url == '/comments') {
            $url = '/comments/comments/1';
        }
        return $url;
    }
}

?>
